/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package udp2serial;
import java.io.*;
import java.net.*;
import java.util.*;

/**
 *
 * @author axelsben
 */
public class client {
    private static String serverAddress = "localhost";
    private static int useSocket = 4774;
    private static javax.swing.JTextArea jta = null;
    private static boolean logging = false;

    public static void setLogging(boolean bool)
    {
        logging = bool;
    }

    public static void setTA(javax.swing.JTextArea jtxta)
    {
        jta = jtxta;
    }

    public static void setAddress(String address)
    {
        serverAddress = address;
    }

    public static void setSocket(int socket)
    {
        useSocket = socket;
    }

    public static String getAddress()
    {
        return serverAddress;
    }

    public static int getSocket()
    {
        return useSocket;
    }

    public static void send(String[] args) throws IOException {

            // get a datagram socket
        DatagramSocket socket = new DatagramSocket();
            // send request
        byte[] buf = new byte[256];
        InetAddress address = InetAddress.getByName(serverAddress);

        for(int i=0; i < args.length;i++)
        {
            buf = args[i].getBytes();
            DatagramPacket packet = new DatagramPacket(buf, buf.length, address, useSocket);
            socket.send(packet);
            //String s = jta.getText();
            //jta.setText("|udp->UDP: " + args[i] + s + "\n");
            if(logging)
            {
                jta.setSelectionStart(jta.getText().length());
                jta.setSelectionEnd(jta.getText().length()+1);
                jta.replaceSelection("/ut:" + args[i]);
                if(jta.getText().length() > 25000)
                {
                    int rm = jta.getText().length() - 25000;
                    jta.setSelectionStart(0);
                    jta.setSelectionEnd(rm);
                    jta.replaceSelection("");
                }
                jta.setSelectionStart(jta.getText().length());
                jta.setSelectionEnd(jta.getText().length()+1);
            }

        }
        socket.close();
    }

}
